<%--
    Document   : index
    Created on : Jun 20, 2012, 3:25:18 AM
    Author     : aspr0
--%>
<%@page import="is.model.dao.RegionDao"%>
<%@page import="is.interfaces.Dao"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.LinkedList"%>
<%@page import="is.web.GetParam"%>
<%@page import="is.web.table.RegionTable"%>
<%@page import="is.web.Message"%>
<%@page import="java.util.List"%>
<%@page import="is.model.bean.Region"%>
<%@page import="is.model.DataSource"%>
<%@page import="java.util.Iterator"%>
<%@page import="is.model.bean.Employee"%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding(is.Conf.ENCODING); // !!!

    String datatype = "";
    String content = "";

    String msgInfo = Message.genMsgInfo(request.getParameter(GetParam.INFO));
    String msgWarning = Message.genMsgWarning(request.getParameter(GetParam.WARNING));
    String msgError = Message.genMsgError(request.getParameter(GetParam.ERROR));

    try
    {
        List<Region> regions;
        RegionTable regionTable = new RegionTable();
        regionTable.setInitialBean(new Region());
        regionTable.setOrderBy(request.getParameter(GetParam.ORDER_BY));

        datatype = regionTable.getDatatypeName();

        DataSource.establishConnection();
        Dao<Region> regionDao = new RegionDao();

        if (request.getMethod().equals("POST"))
        {
%>

<jsp:useBean id="searchRegionBean" class="is.model.bean.Region"/>
<jsp:setProperty name="searchRegionBean" property="*"/>

<%
            regionDao.setInitialBean(searchRegionBean);
            regions = regionDao.search(false);
            regionTable.setInitialBean(searchRegionBean);
            regionTable.setHasSearched(true);

            if (regions.size() < 1)
            {
                msgWarning = Message.genMsgWarning("3");
            }
            else
            {
                msgInfo = Message.genMsgInfo("3");
            }
        }
        else
        {
            regionDao.setInitialBean(new Region());
            Integer regionCount = regionDao.count();

            if (regionCount < 1)
            {
                msgWarning = Message.genMsgWarning("4");
                regions = new LinkedList<Region>();
            }
            else
            {
                regions = regionDao.retrieveMultiple(0, regionCount, regionTable.getOrderBy());
            }
        }

        regionTable.setBeans(regions);

        String table = regionTable.genTable();
        DataSource.closeConnection();

        String msg = msgInfo + msgWarning + msgError;
        content = msg + table;
    }
    catch (SQLException e)
    {
        e.printStackTrace();
        content = Message.genMsgError("0") + content;
    }
    catch (Exception e)
    {
        if(e.getCause().getCause().getClass() == new NumberFormatException().getClass())
        {
            content = Message.genMsgWarning("0") + content;
        }
        else
        {
            e.printStackTrace();
            response.sendRedirect("?"+GetParam.ERROR+"=1");
        }
    }
%>

<jsp:include page="../template/index.jsp">
    <jsp:param name="datatype" value="<%=datatype%>"/>
    <jsp:param name="content" value="<%=content%>"/>
</jsp:include>
